//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2011 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ALGORITHM_REGEX_H_
#define _BUOLA_ALGORITHM_REGEX_H_

#include <boost/regex.hpp>

namespace buola {

///\addtogroup algorithm
///@{
    
///\name Regular expressions
///\todo change to std once gcc 4.6 is widely available and fixed
//@{
    
///returns \c true if the regular expression \p pE is found within \p pSrc

///If a match is found, \p pDst contains the contents of the \p pMatch match
inline bool simple_regex_search(const boost::regex &pE,const std::string &pSrc,std::string &pDst,int pMatch=0)
{
    boost::match_results<std::string::const_iterator> lMatches;

    if(boost::regex_search(pSrc,lMatches,pE)&&pMatch<lMatches.size()&&lMatches[pMatch].matched)
    {
        pDst.assign(lMatches[pMatch].first,lMatches[pMatch].second);
        return true;
    }
    else
    {
        pDst.clear();
        return false;
    }
}

///returns \c true if the regular expression \p pRegex is found within \p pSrc

///If a match is found, \p pDst contains the contents of the \p pMatch match
inline bool simple_regex_search(const std::string &pRegex,const std::string &pSrc,std::string &pDst,int pMatch=0)
{
    return simple_regex_search(boost::regex(pRegex),pSrc,pDst,pMatch);
}

//@}

///@{

/*namespace buola*/ }

#endif
